package com.zy.algorithm.v3.array

import android.os.Bundle
import com.zy.view.DIYSortView
import com.zy.utils.data.Point
import com.zy.utils.data.auto.AutoIndexElement
import com.zy.utils.data.auto.AutoNodeStep
import com.zy.utils.data.auto.Coordinate
import java.util.ArrayList

//https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
// 26. 删除有序数组中的重复项
open class D3LeetCode0026 : BaseV3ArrayFragment() {

    private var array1 = intArrayOf(0, 0, 1, 1, 1, 2, 3, 3, 4, 4)

    override fun getInitD(): Int {
        return 150
    }

    private var baseX = 3
    var baseY = 2


    //33
    private var indexIDes: Point =
        builder.setOriginX(baseX - 1).setOriginY(baseY + 1).setV("i=").setIndex(1000)
            .setVTextSize(48).build(Point.Type.DEFAULT)

    private var indexI: Point =
        builder.setOriginX(baseX).setOriginY(baseY + 1).setV("^").setIndex(10001)
            .build(Point.Type.SUB_SCRIPT)

    //34
    private var indexJDes: Point =
        builder.setOriginX(baseX - 1).setOriginY(baseY + 2).setV("j=").setIndex(1000)
            .setVTextSize(48).build(Point.Type.DEFAULT)
    private var indexJ: Point =
        builder.setOriginX(baseX).setOriginY(baseY + 2).setV("^").setIndex(10002)
            .build(Point.Type.SUB_SCRIPT)

    //35
    private var curNodeDes: Point =
        builder.setOriginX(baseX - 1).setOriginY(baseY + 3).setV("curV").setIndex(1000)
            .setVTextSize(48).build(Point.Type.DEFAULT)

    private var curNode: Point =
        builder.setOriginX(baseX).setOriginY(baseY + 3).setV("0").setIndex(10003)
            .build(Point.Type.DEFAULT)

    //36
    private var lastNodeDes: Point =
        builder.setOriginX(baseX - 1).setOriginY(baseY + 4).setV("lastV").setIndex(1000)
            .setVTextSize(48).build(Point.Type.DEFAULT)

    private var lastNode: Point =
        builder.setOriginX(baseX).setOriginY(baseY + 4).setV("").setIndex(10004)
            .build(Point.Type.DEFAULT)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun setData(diyV4: DIYSortView) {
        title.text = "26. 删除有序数组中的重复项"
        diyV4.create(3, 2, array1, Point.Type.DEFAULT)
        diyV4.changeType = DIYSortView.TYPE_COVER

        addAss()
    }

    private fun addAss() {
        diyV4.addPoint(indexIDes)
        diyV4.addPoint(indexI)
        diyV4.addPoint(indexJDes)
        diyV4.addPoint(indexJ)
//        diyV4.addAss(curNodeDes)
//        diyV4.addAss(curNode)
        diyV4.addPoint(lastNodeDes)
        diyV4.addPoint(lastNode)
        diyV4.postInvalidate()
    }

    override fun autoClick() {
        super.autoClick()
        //addNext()
    }


    var i = 0
    var j = 1
    var lastV = array1[0]
    private fun addNext() {
        val curNodeV = array1[i]


        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()




        if (lastV != curNodeV) {
            lastV = curNodeV
            diyV4.findNodeByIndex(j)?.v = lastV.toString()

            i++
            j++
        } else {
            i++
        }

        indexI.v = i.toString()
        curNode.v = curNodeV.toString()
        indexJ.v = j.toString()
        lastNode.v = lastV.toString()


        val stepList = listOf(
            AutoIndexElement(indexI.index, Coordinate(baseX + i, indexI.originY)),
            AutoIndexElement(curNode.index, Coordinate(baseX + i, curNode.originY)),

            AutoIndexElement(indexJ.index, Coordinate(baseX + j, indexJ.originY)),
            AutoIndexElement(lastNode.index, Coordinate(baseX + j, lastNode.originY)),

            )
        list.add(AutoNodeStep(stepList))


        diyV4.autoMove(list)
    }
}